package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.COutcome;
import com.example.entity.First;
import com.example.entity.Outcome;
import com.example.mapper.FirstMapper;
import com.example.mapper.OutcomeMapper;
import com.example.service.IOutcomeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 收入表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-10-29
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class OutcomeServiceImpl extends ServiceImpl<OutcomeMapper, Outcome> implements IOutcomeService {

    private final OutcomeMapper outcomeMapper;

    private final FirstMapper firstMapper;
    @Override
    public List<Double> details(LocalDate begin, LocalDate end) {
        List<Double> amounts = new ArrayList<>();
        Long month = begin.until(end, ChronoUnit.MONTHS);
        log.info("month:{}",month);
        if(month == 1){
            long days = end.toEpochDay() - begin.toEpochDay();
            for (int i = 0; i < days; i++) {
                Double amount = outcomeMapper.getAmounts1(begin);
                begin = begin.plusDays(1);
                amounts.add(amount);
            }
        }else{
            for (int i = 0; i < month; i++) {
                Double amount = outcomeMapper.getAmounts2(begin, begin.plusMonths(1));
                begin = begin.plusMonths(1);
                amounts.add(amount);
            }
        }
        return amounts;
    }

    @Override
    public List<COutcome> classified(LocalDate begin, LocalDate end) {
        List<COutcome> outcomeList = new ArrayList<>();
        List<First> firsts = firstMapper.selectList(new LambdaQueryWrapper<First>()
                .eq(First::getJudge, 1));
        for (First first : firsts) {
            COutcome cOutcome = new COutcome();
            cOutcome.setName(first.getTypeName());
            cOutcome.setValue(outcomeMapper.getAmounts4(begin,end,first.getId()));
            outcomeList.add(cOutcome);
        }
        return outcomeList;
    }

    @Override
    public Double sumByTime(LocalDate begin, LocalDate end) {
        return outcomeMapper.getAmounts2(begin,end);
    }
}
